package com.wn.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wn.common.PageParam;
import com.wn.mapper.*;
import com.wn.ov.*;
import com.wn.pojo.*;
import com.wn.resopnse.ResponseCode;
import com.wn.resopnse.ServerResponse;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

@Service
public class CostService {
    @Autowired
    private CostMapper costMapper;//药品的价格
    @Autowired
    private CostDrugMapper costDrugMapper;//药品的价格
    @Autowired
    private DrugMapper drugMapper;
    @Autowired
    private CostOtherMapper costOtherMapper;//其他的清单

    @Autowired
    private CardMapper cardMapper;

    @Autowired
    private ConsumptionRecordMapper crm;


    //添加药品
    public ServerResponse addCostDrugList(Map<String, Object> map, UserOv user) {
        //map集合取出对象
        DruglistOv drugListOv = null;
        PatientOv patientOv = null;
        for (Map.Entry<String, Object> entry : map.entrySet()) {
            if ("patientOv".equals(entry.getKey())) {
                JSONObject jsonObject = JSONObject.fromObject(entry.getValue()); // 将数据转成json字符串
                patientOv = (PatientOv) JSONObject.toBean(jsonObject, PatientOv.class); //将json转成需要的对象
            } else if ("drugListOv".equals(entry.getKey())) {
                JSONObject jsonObject = JSONObject.fromObject(entry.getValue()); // 将数据转成json字符串
                drugListOv = (DruglistOv) JSONObject.toBean(jsonObject, DruglistOv.class); //将json转成需要的对象
            }
        }
        if (StringUtils.isEmpty(patientOv)) {
            return new ServerResponse(ResponseCode.ERROR, "请先选择患者");
        }

        Drug drug = drugMapper.getDrugByName(drugListOv.getName());
        drugListOv.setDrugId(drug.getId());
        if (StringUtils.isEmpty(drugListOv.getName()) || StringUtils.isEmpty(drugListOv.getNumber())) {
            return new ServerResponse(ResponseCode.ERROR, "药品或药品数量不能为空");
        } else if (drugListOv.getNumber()==0) {
            return new ServerResponse(ResponseCode.ERROR, "药品数量不合法");
        }

        int costId = 0;
        try {
            costId = costMapper.selectByPatientIdAndUnchecked(patientOv.getId()).getId();
        } catch (NullPointerException e) {
            Cost cost = new Cost();
            cost.setCreateUserId(user.getId());
            cost.setPatientId(patientOv.getId());
            costMapper.insertSelective(cost);
            costId = costMapper.selectByPatientIdAndUnchecked(patientOv.getId()).getId();
        }
        List<Drug> drugs = drugMapper.superSearch(drugListOv.getName());
        drugListOv.setDrugId(drugs.get(0).getId());
        drugListOv.setPrice(drugs.get(0).getPrice());

        drugListOv.setDoctorId(user.getId());
        drugListOv.setCostId(costId);
        //获取数量和单价
        Integer number = drugListOv.getNumber();
        BigDecimal price = drugMapper.selectByPrimaryKey(drugListOv.getDrugId()).getPrice();
        BigDecimal multiply = price.multiply(new BigDecimal(number));
        //添加小计金额
        drugListOv.setSubtotal(multiply);
        //调用添加方法
        int insert = costDrugMapper.insertSelective(drugListOv);
        if (insert != 0) {
            //将所用药品库存更新
            drugMapper.updateStockById(drugListOv.getDrugId(), drugMapper.selectByPrimaryKey(drugListOv.getDrugId()).getStock() - drugListOv.getNumber());
            this.updataTotal(patientOv.getId());
            return new ServerResponse(ResponseCode.SUCCESS, "添加成功");
        } else {
            return new ServerResponse(ResponseCode.ERROR, "添加失败");
        }
    }

    public ServerResponse getDrugList(Map<String, Object> map) {
        //map集合取出对象
        PageParam<DruglistOv> pageParam = null;
        PatientOv patientOv = null;
        for (Map.Entry<String, Object> entry : map.entrySet()) {
            if ("patientOv".equals(entry.getKey())) {
                JSONObject jsonObject = JSONObject.fromObject(entry.getValue()); // 将数据转成json字符串
                patientOv = (PatientOv) JSONObject.toBean(jsonObject, PatientOv.class); //将json转成需要的对象
            } else if ("pageParam".equals(entry.getKey())) {
                JSONObject jsonObject = JSONObject.fromObject(entry.getValue()); // 将数据转成json字符串
                pageParam = (PageParam<DruglistOv>) JSONObject.toBean(jsonObject, PageParam.class); //将json转成需要的对象
            }
        }
        if (StringUtils.isEmpty(patientOv)) {
            return new ServerResponse(ResponseCode.ERROR, "请先选择患者");
        }
        if (!StringUtils.isEmpty(pageParam.getSuperSearchKeyWord())) {
            //执行高级检索
            //分页
            PageHelper.startPage(pageParam.getPageNum(), pageParam.getPageSize());
            //排序
            for (int i = 0; i < pageParam.getOrderParams().length; i++) {
                PageHelper.orderBy(pageParam.getOrderParams()[i]);
            }

            List<DruglistOv> ovList = costDrugMapper.superSearchDrug(pageParam.getSuperSearchKeyWord(), patientOv.getId());
            for (DruglistOv druglistOv : ovList) {

                if (druglistOv.getStatus() == 0) {
                    druglistOv.setStatu("未发药");
                } else if (druglistOv.getStatus() == 1) {
                    druglistOv.setStatu("已发药");
                }else if (druglistOv.getStatus() == 2) {
                    druglistOv.setStatu("已支付");
                }
            }
            PageInfo<DruglistOv> pageInfo = new PageInfo<DruglistOv>(ovList);
            return new ServerResponse(ResponseCode.SUCCESS, pageInfo, "查找成功");

        } else {
            //分页
            PageHelper.startPage(pageParam.getPageNum(), pageParam.getPageSize());
            //排序
            for (int i = 0; i < pageParam.getOrderParams().length; i++) {
                PageHelper.orderBy(pageParam.getOrderParams()[i]);
            }
            List<DruglistOv> ovList = costDrugMapper.getCostDrug(patientOv.getId());
            for (DruglistOv druglistOv : ovList) {

                if (druglistOv.getStatus() == 0) {
                    druglistOv.setStatu("未发药");
                } else if (druglistOv.getStatus() == 1) {
                    druglistOv.setStatu("已发药");
                }else if (druglistOv.getStatus() == 2) {
                    druglistOv.setStatu("已支付");
                }
            }
            PageInfo<DruglistOv> pageInfo = new PageInfo(ovList);
            return new ServerResponse(ResponseCode.SUCCESS, pageInfo, "查找成功");
        }
    }

    public ServerResponse updateDrugList(Map<String, Object> map, UserOv user) {
        //map集合取出对象
        DruglistOv drugListOv = null;
        PatientOv patientOv = null;
        for (Map.Entry<String, Object> entry : map.entrySet()) {
            if ("patientOv".equals(entry.getKey())) {
                JSONObject jsonObject = JSONObject.fromObject(entry.getValue()); // 将数据转成json字符串
                patientOv = (PatientOv) JSONObject.toBean(jsonObject, PatientOv.class); //将json转成需要的对象
            } else if ("drugListOv".equals(entry.getKey())) {
                JSONObject jsonObject = JSONObject.fromObject(entry.getValue()); // 将数据转成json字符串
                drugListOv = (DruglistOv) JSONObject.toBean(jsonObject, DruglistOv.class); //将json转成需要的对象
            }
        }
        if (StringUtils.isEmpty(patientOv)) {
            return new ServerResponse(ResponseCode.ERROR, "请先选择患者");
        }
        Drug drug = drugMapper.getDrugByName(drugListOv.getName());
        drugListOv.setDrugId(drug.getId());

        if (StringUtils.isEmpty(drugListOv.getName()) || StringUtils.isEmpty(drugListOv.getNumber())) {
            return new ServerResponse(ResponseCode.ERROR, "药品或药品数量不能为空");
        }  else if (drugListOv.getNumber()==0) {
            return new ServerResponse(ResponseCode.ERROR, "药品数量不合法");
        }

        int costId = costMapper.selectByPatientIdAndUnchecked(patientOv.getId()).getId();
        drugListOv.setDoctorId(user.getId());
        drugListOv.setCostId(costId);

        //获取数量和单价
        Integer number = drugListOv.getNumber();
        BigDecimal price = drugMapper.selectByPrimaryKey(drugListOv.getDrugId()).getPrice();
        BigDecimal multiply = price.multiply(new BigDecimal(number));
        //添加小计金额
        drugListOv.setSubtotal(multiply);
        //调用添加方法
        int insert = costDrugMapper.updateByPrimaryKeySelective(drugListOv);
        if (insert != 0) {
            //将所用药品库存更新
            drugMapper.updateStockById(drugListOv.getDrugId(), drugMapper.selectByPrimaryKey(drugListOv.getDrugId()).getStock() - drugListOv.getNumber());
            this.updataTotal(patientOv.getId());
            return new ServerResponse(ResponseCode.SUCCESS, "修改成功");
        } else {
            return new ServerResponse(ResponseCode.ERROR, "修改失败");
        }
    }

    public ServerResponse deleteDrugList(Map<String, Object> map) {
        //map集合取出对象
        DruglistOv drugListOv = null;
        PatientOv patientOv = null;
        for (Map.Entry<String, Object> entry : map.entrySet()) {
            if ("patientOv".equals(entry.getKey())) {
                JSONObject jsonObject = JSONObject.fromObject(entry.getValue()); // 将数据转成json字符串
                patientOv = (PatientOv) JSONObject.toBean(jsonObject, PatientOv.class); //将json转成需要的对象
            } else if ("drugListOv".equals(entry.getKey())) {
                JSONObject jsonObject = JSONObject.fromObject(entry.getValue()); // 将数据转成json字符串
                drugListOv = (DruglistOv) JSONObject.toBean(jsonObject, DruglistOv.class); //将json转成需要的对象
            }
        }

        int result = costDrugMapper.deleteByPrimaryKey(drugListOv.getId());
        if (result != 0) {
            this.updataTotal(patientOv.getId());
            return new ServerResponse(ResponseCode.SUCCESS, "成功");
        } else {
            return new ServerResponse(ResponseCode.ERROR, "失败");
        }
    }


    public ServerResponse getOtherList(Map<String, Object> map) {
        //map集合取出对象
        PageParam<CostOther> pageParam = null;
        PatientOv patientOv = null;
        for (Map.Entry<String, Object> entry : map.entrySet()) {
            if ("patientOv".equals(entry.getKey())) {
                JSONObject jsonObject = JSONObject.fromObject(entry.getValue()); // 将数据转成json字符串
                patientOv = (PatientOv) JSONObject.toBean(jsonObject, PatientOv.class); //将json转成需要的对象
            } else if ("pageParam".equals(entry.getKey())) {
                JSONObject jsonObject = JSONObject.fromObject(entry.getValue()); // 将数据转成json字符串
                pageParam = (PageParam<CostOther>) JSONObject.toBean(jsonObject, PageParam.class); //将json转成需要的对象
            }
        }
        if (StringUtils.isEmpty(patientOv)) {
            return new ServerResponse(ResponseCode.ERROR, "请先选择患者");
        }
        if (!StringUtils.isEmpty(pageParam.getSuperSearchKeyWord())) {
            //执行高级检索
            //分页
            PageHelper.startPage(pageParam.getPageNum(), pageParam.getPageSize());
            //排序
            for (int i = 0; i < pageParam.getOrderParams().length; i++) {
                PageHelper.orderBy(pageParam.getOrderParams()[i]);
            }

            List<CostOtherOv> ovList = costOtherMapper.superSearchOther(pageParam.getSuperSearchKeyWord(), patientOv.getId());
            PageInfo<CostOtherOv> pageInfo = new PageInfo<CostOtherOv>(ovList);
            return new ServerResponse(ResponseCode.SUCCESS, pageInfo, "查找成功");

        } else {
            //分页
            PageHelper.startPage(pageParam.getPageNum(), pageParam.getPageSize());
            //排序
            for (int i = 0; i < pageParam.getOrderParams().length; i++) {
                PageHelper.orderBy(pageParam.getOrderParams()[i]);
            }
            List<CostOtherOv> ovList = costOtherMapper.getOtherDrug(patientOv.getId());
            System.out.println(ovList);
            PageInfo<CostOtherOv> pageInfo = new PageInfo(ovList);
            return new ServerResponse(ResponseCode.SUCCESS, pageInfo, "查找成功");
        }
    }

    public ServerResponse updateOtherList(Map<String, Object> map, UserOv user) {
        //map集合取出对象
        DruglistOv druglistOv = null;
        PatientOv patientOv = null;
        for (Map.Entry<String, Object> entry : map.entrySet()) {
            if ("patientOv".equals(entry.getKey())) {
                JSONObject jsonObject = JSONObject.fromObject(entry.getValue()); // 将数据转成json字符串
                patientOv = (PatientOv) JSONObject.toBean(jsonObject, PatientOv.class); //将json转成需要的对象
            } else if ("druglistOv".equals(entry.getKey())) {
                JSONObject jsonObject = JSONObject.fromObject(entry.getValue()); // 将数据转成json字符串
                druglistOv = (DruglistOv) JSONObject.toBean(jsonObject, DruglistOv.class); //将json转成需要的对象
            }
        }
        CostOther costOther = new CostOther();
        costOther.setId(druglistOv.getId());
        costOther.setName(druglistOv.getName());
        costOther.setPrice(druglistOv.getPrice());
        costOther.setNumber(druglistOv.getNumber());


        if (StringUtils.isEmpty(costOther.getName())||StringUtils.isEmpty(costOther.getNumber())||StringUtils.isEmpty(costOther.getPrice())){
            return new ServerResponse(ResponseCode.ERROR,"输入不能为空");
        }else if (costOther.getNumber()==0){
            return new ServerResponse(ResponseCode.ERROR,"数量不合法");
        }

        int costId = costMapper.selectByPatientIdAndUnchecked(patientOv.getId()).getId();


        costOther.setDoctorId(user.getId());
        costOther.setCostId(costId);
        //获取数量和单价
        Integer number = costOther.getNumber();
        BigDecimal price = costOther.getPrice();
        BigDecimal multiply = price.multiply(new BigDecimal(number));
        //添加小计金额
        costOther.setSubtotal(multiply);
        System.out.println("---------2----------"+costOther.getNumber());

        //调用添加方法
        int insert = costOtherMapper.updateByPrimaryKeySelective(costOther);
        if (insert != 0) {
            this.updataTotal(patientOv.getId());
            return new ServerResponse(ResponseCode.SUCCESS, "修改成功");
        } else {
            return new ServerResponse(ResponseCode.ERROR, "修改失败");
        }
    }

    //修改其费用的状态
    public ServerResponse updateCost(Cost cost, Integer id) {
        cost.setStatus(2);
        cost.setUpdateUserId(id);
        int i = costMapper.updateByPrimaryKeySelective(cost);
        if (i != 0) {
            return new ServerResponse(ResponseCode.SUCCESS, "成功");
        } else {
            return new ServerResponse(ResponseCode.ERROR, "失败");
        }
    }

    public ServerResponse addOtherList(Map<String, Object> map, UserOv user) {
        //map集合取出对象
        CostOther costOther = null;
        PatientOv patientOv = null;
        for (Map.Entry<String, Object> entry : map.entrySet()) {
            if ("patientOv".equals(entry.getKey())) {
                JSONObject jsonObject = JSONObject.fromObject(entry.getValue()); // 将数据转成json字符串
                patientOv = (PatientOv) JSONObject.toBean(jsonObject, PatientOv.class); //将json转成需要的对象
            } else if ("costOther".equals(entry.getKey())) {
                JSONObject jsonObject = JSONObject.fromObject(entry.getValue()); // 将数据转成json字符串
                costOther = (CostOther) JSONObject.toBean(jsonObject, CostOther.class); //将json转成需要的对象
            }
        }
        //查询cost表是否有数据 若无 则添加一条
        if (StringUtils.isEmpty(patientOv)) {
            return new ServerResponse(ResponseCode.ERROR, "请先选择患者");
        }
        if (StringUtils.isEmpty(costOther.getName())||StringUtils.isEmpty(costOther.getNumber())||StringUtils.isEmpty(costOther.getPrice())){
            return new ServerResponse(ResponseCode.ERROR,"输入不能为空");
        }else if (costOther.getNumber()==0){
            return new ServerResponse(ResponseCode.ERROR,"数量不合法");
        }
        int costId = 0;
        try {
            costId = costMapper.selectByPatientIdAndUnchecked(patientOv.getId()).getId();
        } catch (NullPointerException e) {
            Cost cost = new Cost();
            cost.setCreateUserId(user.getId());
            cost.setPatientId(patientOv.getId());
            costMapper.insertSelective(cost);
            costId = costMapper.selectByPatientIdAndUnchecked(patientOv.getId()).getId();
        }

        costOther.setDoctorId(user.getId());
        costOther.setCostId(costId);
        //获取数量和单价
        Integer number = costOther.getNumber();
        BigDecimal price = costOther.getPrice();
        BigDecimal multiply = price.multiply(new BigDecimal(number));
        //添加小计金额
        costOther.setSubtotal(multiply);
        //调用添加方法
        int insert = costOtherMapper.insertSelective(costOther);
        if (insert != 0) {
//            this.updataTotal(patientOv.getId());
            return new ServerResponse(ResponseCode.SUCCESS, "添加成功");
        } else {
            return new ServerResponse(ResponseCode.ERROR, "添加失败");
        }
    }

    public ServerResponse deleteOtherList(Map<String, Object> map) {
        //map集合取出对象
        CostOther costOther = null;
        PatientOv patientOv = null;
        for (Map.Entry<String, Object> entry : map.entrySet()) {
            if ("patientOv".equals(entry.getKey())) {
                JSONObject jsonObject = JSONObject.fromObject(entry.getValue()); // 将数据转成json字符串
                patientOv = (PatientOv) JSONObject.toBean(jsonObject, PatientOv.class); //将json转成需要的对象
            } else if ("costOther".equals(entry.getKey())) {
                JSONObject jsonObject = JSONObject.fromObject(entry.getValue()); // 将数据转成json字符串
                costOther = (CostOther) JSONObject.toBean(jsonObject, CostOther.class); //将json转成需要的对象
            }
        }
        //查询cost表是否有数据 若无 则添加一条
        if (StringUtils.isEmpty(patientOv)) {
            return new ServerResponse(ResponseCode.ERROR, "请先选择患者");
        }

        int result = costOtherMapper.deleteByPrimaryKey(costOther.getId());
        if (result != 0) {
            return new ServerResponse(ResponseCode.SUCCESS, "成功");
        } else {
            return new ServerResponse(ResponseCode.ERROR, "失败");
        }
    }

    /**
     * 计算小计以及总计
     *
     * @param patientId
     */
    public void updataTotal(int patientId) {
        BigDecimal bigDecimal = new BigDecimal("0");
        Cost cost = costMapper.selectByPatientId(patientId);
        List<CostDrug> costDrugList = costDrugMapper.selectByCostId(cost.getId());
        List<CostOther> costOtherList = costOtherMapper.selectByCostId(cost.getId());
        for (CostDrug costDrug : costDrugList) {
            //从药品表查询药品价格并且更新
            costDrug.setPrice(drugMapper.selectByPrimaryKey(costDrug.getDrugId()).getPrice());
            BigDecimal subtotal = costDrug.getPrice().multiply(new BigDecimal(costDrug.getNumber()));
            costDrug.setSubtotal(subtotal);
            costDrugMapper.updateByPrimaryKeySelective(costDrug);
            bigDecimal = bigDecimal.add(subtotal);
        }
        for (CostOther costOther : costOtherList) {
            System.out.println(costOther.getNumber());
            BigDecimal subtotal = costOther.getPrice().multiply(new BigDecimal(costOther.getNumber()));
            costOther.setSubtotal(subtotal);
            costOtherMapper.updateByPrimaryKeySelective(costOther);
            bigDecimal = bigDecimal.add(subtotal);
        }
        cost.setTotalMoney(bigDecimal);
        costMapper.updateByPrimaryKeySelective(cost);
    }


    //查询药品费用的详情
    public ServerResponse findCostDrugByPatientId(Integer patientId) {
        List<DruglistOv> list = costDrugMapper.findCostDrugByPatientId(patientId);
        if (list != null) {
            return new ServerResponse(ResponseCode.SUCCESS, list, "成功");
        } else {
            return new ServerResponse(ResponseCode.ERROR, "查找失败");
        }
    }

    //查询药品费用的详情
    public ServerResponse findCostOtherByPatientId(Integer patientId) {
        List<CostOtherOv> list = costOtherMapper.findCostOtherByPatientId(patientId);
        if (list != null) {
            return new ServerResponse(ResponseCode.SUCCESS, list, "成功");
        } else {
            return new ServerResponse(ResponseCode.ERROR, "查找失败");
        }
    }

    //查询总的费用
    public ServerResponse findCostByPatientId(Integer patientId) {
        Cost cost = costMapper.selectByPatientId(patientId);
        if (StringUtils.isEmpty(cost)) {
            return new ServerResponse(ResponseCode.ERROR, "查找失败");
        } else {
            return new ServerResponse(ResponseCode.SUCCESS, cost, "成功");
        }
    }

    //审核成功出单
    public ServerResponse updateDrugStatus1(CostDrug costDrug,Integer id) {
        int i = costDrugMapper.updateDrugStatus1(costDrug.getId());
        //通过id来查找药品费用
        CostDrug costDrug1 = costDrugMapper.selectByPrimaryKey(costDrug.getId());
        Cost cost = costMapper.selectByPrimaryKey(costDrug1.getCostId());


        //修改数据库状态
        //状态为0
        List<CostDrug> costDrugs2 = costDrugMapper.selectByCostId2(costDrug1.getCostId());
        if(costDrugs2.size()==0){
            cost.setStatus(0);
            cost.setUpdateUserId(id);
            costMapper.updateByPrimaryKeySelective(cost);
            updataTotal(cost.getPatientId());
        }

        if (i != 0) {
            return new ServerResponse(ResponseCode.SUCCESS, "审核通过");
        } else {
            return new ServerResponse(ResponseCode.ERROR, "审核不通过");
        }
    }

    //取消审核出单
    public ServerResponse updateDrugStatus2(CostDrug costDrug) {
        int i = costDrugMapper.updateDrugStatus2(costDrug.getId());
        CostDrug costDrug1 = costDrugMapper.selectByPrimaryKey(costDrug.getId());
        Cost cost = new Cost();
        cost.setId(costDrug1.getCostId());
        cost.setTotalMoney(new BigDecimal(0));
        costMapper.updateByPrimaryKeySelective(cost);

        if (i != 0) {
            return new ServerResponse(ResponseCode.SUCCESS, "取消审核成功");
        } else {
            return new ServerResponse(ResponseCode.ERROR, "取消审核失败");
        }
    }

    //查询总的费用(分页多表)
    public ServerResponse getCostList(PageParam<CostOv> pageParam) {
        if (!StringUtils.isEmpty(pageParam.getSuperSearchKeyWord())) {
            //分页
            PageHelper.startPage(pageParam.getPageNum(), pageParam.getPageSize());

            List<CostOv> costOvList = costMapper.superSearch(pageParam.getSuperSearchKeyWord());

            //判断是否有医保卡
            for(CostOv co :costOvList){
                Card card = cardMapper.selectByPatientId(co.getPatientId());
                if(StringUtils.isEmpty(card)){
                    co.setFlag(1);
                }else{
                    co.setFlag(0);
                }
            }

            PageInfo<CostOv> costOvPageInfo = new PageInfo<CostOv>(costOvList);
            System.out.println(costOvPageInfo);
            return new ServerResponse(ResponseCode.SUCCESS, costOvPageInfo);
        } else {
            //分页
            PageHelper.startPage(pageParam.getPageNum(), pageParam.getPageSize());
            List<CostOv> list = costMapper.selectAll();

            //判断是否有医保卡
            for(CostOv co :list){
                Card card = cardMapper.selectByPatientId(co.getPatientId());
                if(StringUtils.isEmpty(card)){
                    co.setFlag(1);
                }else{
                    co.setFlag(0);
                }
            }



            //System.out.println(list);
            PageInfo<CostOv> pageInfo = new PageInfo<CostOv>(list);
            return new ServerResponse(ResponseCode.SUCCESS, pageInfo);
        }
    }

    //查询药品的费用(分页多表)
    public ServerResponse getCostDrugDetailList(PageParam<DruglistOv> pageParam) {
        //通过costId来查找
        if (!StringUtils.isEmpty(pageParam.getSuperSearchKeyWord())){
            //分页
            PageHelper.startPage(pageParam.getPageNum(), pageParam.getPageSize());

        List<DruglistOv> costOvList = costDrugMapper.superSearch2(pageParam.getSuperSearchKeyWord(),pageParam.getId());
        PageInfo<DruglistOv> costOvPageInfo = new PageInfo<DruglistOv>(costOvList);
        return new ServerResponse(ResponseCode.SUCCESS, costOvPageInfo);
    }else{
        //分页
        PageHelper.startPage(pageParam.getPageNum(), pageParam.getPageSize());
        List<DruglistOv> list = costDrugMapper.getCostDrugByCostId(pageParam.getId());
        PageInfo<DruglistOv> pageInfo = new PageInfo<DruglistOv>(list);
        return new ServerResponse(ResponseCode.SUCCESS, pageInfo);
    }
    }

    //查询其他的费用(分页多表)
    public ServerResponse getCostOtherList(PageParam<CostOtherOv> pageParam) {
        //通过costId来查找
        if (!StringUtils.isEmpty(pageParam.getSuperSearchKeyWord())){
            //分页
            PageHelper.startPage(pageParam.getPageNum(), pageParam.getPageSize());

            List<CostOtherOv> costOvList = costOtherMapper.superSearch(pageParam.getSuperSearchKeyWord(),pageParam.getId());
            PageInfo<CostOtherOv> costOvPageInfo = new PageInfo<CostOtherOv>(costOvList);
            return new ServerResponse(ResponseCode.SUCCESS, costOvPageInfo);
        }else{
            //分页
            PageHelper.startPage(pageParam.getPageNum(), pageParam.getPageSize());
            List<CostOtherOv> list = costOtherMapper.getCostOther(pageParam.getId());
            PageInfo<CostOtherOv> pageInfo = new PageInfo<CostOtherOv>(list);
            return new ServerResponse(ResponseCode.SUCCESS, pageInfo);
        }
    }

    //医保支付
    public ServerResponse updateCostStatus1(CostOv costOv) {
        Cost cost = new Cost();
        cost.setIsDeleted(1);
        cost.setStatus(1);
        cost.setId(costOv.getId());

        //医保卡的操作
        Cost cost1 = costMapper.selectByPrimaryKey(costOv.getId());
        //通过病人的id来获得其医保卡
        Card card = cardMapper.selectByPatientId(cost1.getPatientId());

        BigDecimal balance = card.getBalance();//现有的余额
        BigDecimal outMoney = cost1.getTotalMoney();//消费
        int i=0;
        if(balance.compareTo(outMoney)==1){
            //修改余额
            card.setBalance(balance.subtract(outMoney));
            cardMapper.updateByPrimaryKeySelective(card);
            //修改状态
             i= costMapper.updateByPrimaryKeySelective(cost);
             //添加消费记录
            ConsumptionRecord cr = new ConsumptionRecord();
            cr.setCardId(card.getId());
            cr.setOutMoney(outMoney);
            cr.setBalance(balance.subtract(outMoney));
            crm.insertSelective(cr);

            //修改药品费用表的状态
            List<CostDrug> costDrugs = costDrugMapper.selectByCostId(cost1.getId());
            for(CostDrug cd : costDrugs){
                cd.setStatus(2);
                costDrugMapper.updateByPrimaryKeySelective(cd);
            }
        }else{
            return new ServerResponse(ResponseCode.ERROR, "余额不足");
        }

        if (i != 0) {
            return new ServerResponse(ResponseCode.SUCCESS, "支付成功");
        } else {
            return new ServerResponse(ResponseCode.ERROR, "支付失败");
        }
    }

    //其他支付
    public ServerResponse updateCostStatus2(CostOv costOv) {
        Cost cost = new Cost();
        cost.setIsDeleted(1);
        cost.setStatus(2);
        cost.setId(costOv.getId());
        int i = costMapper.updateByPrimaryKeySelective(cost);
        if (i != 0) {
            return new ServerResponse(ResponseCode.SUCCESS, "支付成功");
        } else {
            return new ServerResponse(ResponseCode.ERROR, "支付失败");
        }
    }


}
